<template>
  <div class="app-container">
    <el-card class="box-card" shadow="never">
      <el-form :inline="true" :model="dataForm">
        <el-form-item label="公司名称">
          <el-autocomplete
            clearable
            v-model="dataForm.companyName"
            :fetch-suggestions="querySearch"
            placeholder="请输入公司名称"
            :trigger-on-focus="false"
            @select="handleSelect"
          ></el-autocomplete>
        </el-form-item>
        <el-form-item label="申报时间">
          <el-date-picker
            v-model="dataForm.dateTime"
            type="daterange"
            class="time"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptionsItem"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="申报计划所属周期" class="yearWall">
          <el-date-picker
            v-model="dataForm.cycleDateStart"
            type="year"
            value-format="yyyy"
            placeholder="选择年"
          ></el-date-picker>
          <span class="to">至</span>
          <el-date-picker
            v-model="dataForm.cycleDateEnd"
            type="year"
            value-format="yyyy"
            placeholder="选择年"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="box-card" shadow="never">
      <div slot="header">
        <div class="selectWrap">
          <span class="selectTitle">统计维度 :</span>

          <el-select
            class="selectOption"
            v-model="dataForm.companyTypeId"
            @change="handleTypeChange"
            placeholder="请选择公司类型"
          >
            <el-option
              v-for="item in companyTypeList"
              :key="item.id"
              :label="item.companyTypeName"
              :value="item.id"
            ></el-option>
          </el-select>
          <el-select
            class="selectOption"
            v-model="dataForm.type"
            @change="handleChange"
            placeholder="请选择月份"
          >
            <el-option
              v-for="item in publicList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
          <el-select
            class="selectOption"
            v-model="dataForm.waterTypeId"
            @change="handleWaterTypeChange"
            placeholder="请选择用水类型"
          >
            <el-option
              v-for="item in waterTypeList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </div>
      </div>
      <!-- 数据展示 -->
      <el-table
        :data="tableData"
        :header-cell-style="{background:'#f0f3fa',textAlign:'center'}"
        :row-class-name="tableRowClassName"
        :cell-style="{textAlign:'center'}"
        style="width: 100%"
      >
        <el-table-column prop="companyName" label="单位名称" style="w'i"></el-table-column>
        <el-table-column prop="companyTypeName" label="单位类型"></el-table-column>
        <el-table-column prop="waterTypeName" label="用水类型"></el-table-column>
        <el-table-column prop="companyAddress" label="单位地址"></el-table-column>
        <!-- 年 -->
        <el-table-column prop="halfYear" label="上半年" v-if="dataForm.type == 2"></el-table-column>
        <el-table-column prop="secondHalfYear" label="下半年" v-if="dataForm.type == 2"></el-table-column>
        <!-- 季度 -->
        <el-table-column prop="firstQuarter" label="第一季度" v-if="dataForm.type == 1"></el-table-column>
        <el-table-column prop="secondQuarter" label="第二季度" v-if="dataForm.type == 1"></el-table-column>
        <el-table-column prop="thirdQuarter" label="第三季度" v-if="dataForm.type == 1"></el-table-column>
        <el-table-column prop="fourthQuarter" label="第四季度" v-if="dataForm.type == 1"></el-table-column>
        <!-- 月 -->
        <template v-for="(item,index) in tableOption">
          <el-table-column v-if="dataForm.type == 0" width="80" :label="item.label" :key="index">
            <template slot-scope="scope">
              <span>{{scope.row.insertWaterDeclareItemResponses[index].water}}</span>
            </template>
          </el-table-column>
        </template>

        <el-table-column label="用水量">
          <template slot-scope="scope">
            <span class="bule" @click="handleSee(scope.row)">{{ scope.row.totalWater}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="applyDate" label="申报日期"></el-table-column>
        <el-table-column prop="cycleDate" label="申报计划所属周期"></el-table-column>
        <el-table-column label="是否超计划申报">
          <template slot-scope="scope">{{scope.row.planStatus ? '是' :'否'}}</template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="dataForm.page"
        :page-sizes="[10, 20, 40, 60]"
        :page-size="dataForm.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <waterItem ref="waterItem"></waterItem>
  </div>
</template>

<script>
import { getCompanyType, getAllWaterType } from '@/api/base'
import { getReportWater } from '@/api/other'
import waterItem from '@/components/waterItem/idnex.vue'
export default {
  // 组件注册
  components: {
    waterItem,
  },
  data() {
    return {
      dataForm: {
        companyId: '',
        companyName: '',
        dateTime: [this.ShowDate(365), this.ShowDate(0)], // mixins中
        cycleDateStart: '',
        cycleDateEnd: '',
        page: 1,
        pageSize: 10,
        companyTypeId: '',
        type: 2, // 0为月份1为季度，2为年
        waterTypeId: '', // 用水类型
      },
      companyTypeList: [], // 公司类型
      waterTypeList: [], // 用水类型
      publicList: [
        {
          value: 0,
          label: '月份',
        },
        {
          value: 1,
          label: '季度',
        },
        {
          value: 2,
          label: '年度',
        },
      ],
      tableData: [],
      total: 0,
      tableOption: [
        { label: '一月份' },
        { label: '二月份' },
        { label: '三月份' },
        { label: '四月份' },
        { label: '五月份' },
        { label: '六月份' },
        { label: '七月份' },
        { label: '八月份' },
        { label: '九月份' },
        { label: '十月份' },
        { label: '十一月份' },
        { label: '十二月份' },
      ],
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.getCompanyTypeReq() // 公司类型
      this.getAllWaterTypeReq() // 用水类型
      this.getReportWaterFun()
    },
    getReportWaterFun() {
      let data = {
        applyDateEnd: this.dataForm.dateTime[1] + ' 23:59:59',
        applyDateStart: this.dataForm.dateTime[0] + ' 00:00:00',
        companyId: this.dataForm.companyId,
        companyTypeId: this.dataForm.companyTypeId,
        cycleDateEnd: this.dataForm.cycleDateEnd,
        cycleDateStart: this.dataForm.cycleDateStart,
        page: this.dataForm.page,
        pageSize: this.dataForm.pageSize,
        type: this.dataForm.type,
        waterTypeId: this.dataForm.waterTypeId,
      }
      getReportWater(data).then((res) => {
        const { code, data, msg } = res
        if (code !== '0000') return this.$message.error(msg)
        this.total = data.total
        this.tableData = data.list
      })
    },
    getCompanyTypeReq() {
      getCompanyType().then((res) => {
        const { code, data, msg } = res
        if (code !== '0000') return this.$message.error(msg)
        this.companyTypeList = data
      })
    },
    getAllWaterTypeReq() {
      getAllWaterType().then((res) => {
        const { code, data, msg } = res
        if (code !== '0000') return this.$message.error(msg)
        this.waterTypeList = data
      })
    },
    handleSee({ id }) {
      this.$refs.waterItem.init(id)
    },
    onSubmit() {
      this.dataForm.page = 1
      this.dataForm.pageSize = 10
      if (this.dataForm.cycleDateStart > this.dataForm.cycleDateEnd)
        return this.$message.info('开始年大于结束年')
      this.getReportWaterFun()
    },
    handleChange(event) {
      this.dataForm.type = event
      this.getReportWaterFun()
    },
    handleTypeChange(event) {
      this.dataForm.companyTypeId = event
      this.getReportWaterFun()
    },
    handleWaterTypeChange(event) {
      this.dataForm.waterTypeId = event
      this.getReportWaterFun()
    },
    handleSelect(item) {
      this.dataForm.companyId = item.id
    },
    // 监听pageSize改变事件
    handleSizeChange(newSize) {
      this.dataForm.pageSize = newSize
      this.getReportWaterFun()
    },
    // 监听 页面值 改变的事件
    handleCurrentChange(newPage) {
      this.dataForm.page = newPage
      this.getReportWaterFun()
    },
  },
}
</script>

<style lang='scss' scoped>
</style>
